﻿

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Traceability.App.InfrasIntructure
{
    /// <summary>
    /// 仓储层实现
    /// </summary>
    /// <typeparam name="T">数据类型</typeparam>

    public class BaseRepository<T> :IBaseRepository<T> where T : class
    {
       private readonly TraceabilityDbContext db;

        public BaseRepository(TraceabilityDbContext db)
        {
            this.db = db;
        }

        /// <summary>
        /// 添加数据
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns>返回受影响的行数</returns>
        public  Task<int> Add(T entity)
        {
            db.Set<T>().Add(entity);
            return db.SaveChangesAsync();
        }
        /// <summary>
        /// 修改数据
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns>返回受影响的行数</returns>
      public   Task<int> Update(T entity)
        {
            db.Set<T>().Update(entity);
            return db.SaveChangesAsync();
        }
        /// <summary>
        /// 查询所有数据
        /// </summary>
        /// <returns>返回查询到的数据</returns>
      public   IQueryable<T> GetAll()
        {
            return db.Set<T>();
        }

        public Task<int> AddAll(IList<T> t)
        {
            db.Set<T>().AddRange(t);
            return db.SaveChangesAsync();
        }

        public Task<int> UpdateAll(IList<T> t)
        {
            db.Set<T>().UpdateRange(t);
            return db.SaveChangesAsync();
        }
       public async Task<T> GetValue(int id)
        {
            return await db.Set<T>().FindAsync(id);
        }
    }
}
